package gui

import (
	"hardware/info"
	"os"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/flopp/go-findfont"
)

func init() {
	//设置中文字体:解决中文乱码问题
	fontPaths := findfont.List()
	for _, path := range fontPaths {
		if strings.Contains(path, "simkai.ttf") || strings.Contains(path, "msyh.ttf") || strings.Contains(path, "simsun.ttc") || strings.Contains(path, "simhei.ttf") {
			os.Setenv("FYNE_FONT", path)
			break
		}
	}
}

func GuiMain() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Windows系统硬件信息")
	cpu := NewHardware("获取cpu信息!", "cpu", info.MapCPUInfo)
	memory := NewHardware("获取内存信息!", "内存", info.MapPhysicalMemoryInfo)
	baseboard := NewHardware("获取主板信息!", "主板", info.MapBaseBoardInfo)
	disk := NewHardware("获取硬盘信息!", "硬盘", info.MapJsonDiskInfo)
	video := NewHardware("获取显卡信息!", "显卡", info.MapJsonVideoControllerInfo)
	monitor := NewHardware("获取显示器信息!", "显示器", info.MapMonitorInfo)
	bios := NewHardware("获取BIOS信息!", "BIOS", info.MapJsonBiosInfo)
	onBoardDevice := NewHardware("获取板常见适配器设备信息!", "板上常见适配器设备", info.MapOnBoardInfo)
	bus := NewHardware("获取物理总线信息!", "物理总线", info.MapBusInfo)
	net := NewHardware("获取网卡信息!", "物理网卡", info.MapNetInfo)
	battery := NewHardware("获取电池信息!", "电池", info.MapJsonBatteryInfo)
	sound := NewHardware("获取声卡信息!", "声卡", info.MapJsonSoundInfo)

	tabs := container.NewAppTabs(
		container.NewTabItem("主页", widget.NewLabel("World!")),
		container.NewTabItem("cpu信息", container.NewBorder(cpu.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(cpu.Entry)))),
		container.NewTabItem("内存信息", container.NewBorder(memory.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(memory.Entry)))),
		container.NewTabItem("主板信息", container.NewBorder(baseboard.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(baseboard.Entry)))),
		container.NewTabItem("硬盘信息", container.NewBorder(disk.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(disk.Entry)))),
		container.NewTabItem("显卡信息", container.NewBorder(video.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(video.Entry)))),
		container.NewTabItem("显示器信息", container.NewBorder(monitor.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(monitor.Entry)))),
		container.NewTabItem("BIOS信息", container.NewBorder(bios.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(bios.Entry)))),
		container.NewTabItem("板上常见适配器设备信息", container.NewBorder(onBoardDevice.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(onBoardDevice.Entry)))),
		container.NewTabItem("获取物理总线信息", container.NewBorder(bus.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(bus.Entry)))),
		container.NewTabItem("获取网卡信息", container.NewBorder(net.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(net.Entry)))),
		container.NewTabItem("获取电池信息", container.NewBorder(battery.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(battery.Entry)))),
		container.NewTabItem("获取声卡信信息", container.NewBorder(sound.Btnfn(), nil, nil, nil, container.NewStack(container.NewPadded(sound.Entry)))),
	)

	fileMenu := fyne.NewMenu("文件",
		fyne.NewMenuItem("保存", func() {}),
		fyne.NewMenuItem("Settings", func() {}),
	)
	helpMenu := fyne.NewMenu("帮助",
		fyne.NewMenuItem("关于", func() {}),
		fyne.NewMenuItem("说明", func() {}),
		fyne.NewMenuItem("检查更新", func() {}),
	)
	he := fyne.NewMainMenu(fileMenu, helpMenu)
	myWindow.SetMainMenu(he)

	tabs.SetTabLocation(container.TabLocationTop)
	myWindow.SetContent(tabs)
	myWindow.Resize(fyne.NewSize(800, 600))
	myWindow.ShowAndRun()
}
